//
//  OrderManageControl.m
//  dyseller
//
//  Created by 红喇叭 on 2017/8/15.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "OrderManageControl.h"
#import "PayOrderViewController.h"
#import "DateSelectView.h"


@interface OrderManageControl ()<UITextFieldDelegate>{
    NSDictionary *data;
    UILabel *_selectTime;
    UILabel *rightLab;
}


@end

@implementation OrderManageControl
- (void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    __weak typeof(self) weakself = self;
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(10, 0, 44, 44);
    [backBtn setImage:[UIImage imageNamed:@"caiwu_nav_icon_default.png"] forState:UIControlStateNormal];
    if (@available(iOS 11, *)) {
        backBtn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
        [backBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -1 *SCREEN_W /375.0,0,0)];
    }

    [backBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        [weakself.navigationController popViewControllerAnimated:YES];
    }];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    
       if(!_dataString) _dataString = [TimeUtils ToTimeFormat:[NSDate date] TimeFormat:@"yyyy-MM-dd"];
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GETWITH(260), 44)];
    
    
    rightLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, titleView.width-10, 44)];
    rightLab.font = font16;
    rightLab.textAlignment = NSTextAlignmentCenter;
    rightLab.text = [TimeUtils ToTimeFormat:[NSDate date] TimeFormat:@"yyyy-MM-dd"];

    [titleView addSubview:rightLab];

    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dingd_icon7_default_nor.png"]];
    image.frame = CGRectMake(rightLab.right, 0, 7, 4);
    image.centerY = rightLab.centerY;
    [titleView addSubview:image];
    
    self.navigationItem.titleView = titleView;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rqiData)];
    [titleView addGestureRecognizer:tap];
    
   UIButton* rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0,44, 44);
    [rightBtn setImage:[UIImage imageNamed:@"dingd_icon_default_nor.png"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(sreachTitle) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
  
    
    self.segmentBorderColor = UIColorFromRGB(0xE71A41);
    self.segmentTitleColor = UIColorFromRGB(0x333333);
    
    self.segmentHighlightColor = UIColorFromRGB(0xE71A41);
    
    [self loadingdata:[TimeUtils ToTimeFormat:[NSDate date] TimeFormat:@"yyyy-MM-dd"]];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:@"RefundSucceed" object:nil];
}

- (void)loadData{
    
    [self loadingdata:_dataString];
    
}

- (void)rqiData{
    NSString *dateStr = _dataString;
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
    dateFormatter.dateFormat=@"yyyy-MM-dd ";
    NSDate *date =  [dateFormatter dateFromString:dateStr];
    
    
    __weak typeof(self)weakself = self;
    DateSelectView *select = [[DateSelectView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H) selectDate:date];
    select.calendar.didSelectDayHandler = ^(NSInteger year, NSInteger month, NSInteger day) {
        
        NSString *dateTime = [NSString stringWithFormat:@"%ld-%02ld-%02ld",year,month,day];
        weakself.dataString = dateTime;
        rightLab.text = dateTime;
        [weakself loadingdata:dateTime];
        [select removeFromSuperview];
        
    };

    [UIView animateWithDuration:0.5 animations:^{
        [[UIApplication sharedApplication].keyWindow addSubview:select];
    }];
}

- (void)sreachTitle{
    PayOrderViewController *vc = [[PayOrderViewController alloc] initWith:@[] style:1];
    vc.fromVC = 2;
    [self.navigationController pushViewController:vc animated:YES];

}

- (void)loadingdata:(NSString *)date{
    __weak typeof (self)weakself = self;
    [[PPHttpRequsetData shareInstance] requestStyle:Request_POST URL:@"/ShopApi/Order/orderList" parameter:@{@"searchDate":date} successBlock:^(PPHttpRequest *request, id responseObj) {
          data = responseObj;
        [weakself notice];
      
    } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
        [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
    }];
}

// 重新更新菜单
-(void)notice
{
    
    NSMutableArray *arr = [NSMutableArray array];
    NSArray *aray = @[@"买单订单",@"商品订单"];
    
    for(int i = 0;i<2;i++){
        if(i==1){
            PayOrderViewController *vc = [[PayOrderViewController alloc] initWith:data[@"goodsOrder"] style:0];
            vc.title = aray[i];
            [arr addObject:vc];
        }else{
           
          PayOrderViewController *vc = [[PayOrderViewController alloc] initWith:data[@"payOrder"] style:1];
            
            vc.title = aray[i];
            [arr addObject:vc];

        }
    }
    
    
    // 菜单右侧宽度
    self.rightWidth = 0;
    self.topHeight = 0;
    self.viewControllers = arr;
   // [self SelectAtIndex:self.segmentControl.selectIndex];
    
    
    if(_index){
       [self SelectAtIndex:_index];
    }else{
        _index = self.segmentControl.selectIndex;
         [self SelectAtIndex:_index];
    }
    
    
   
}



@end
